package com.senseuni.da.ui.views;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.senseuni.da.R;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.StyleRes;
import androidx.appcompat.content.res.AppCompatResources;

public class SettingItemView2 extends FrameLayout {

    private ImageView iconImageView;
    private TextView titleLabel;
    private TextView summaryLabel;

    public SettingItemView2(@NonNull Context context) {
        this(context, null);
    }

    public SettingItemView2(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingItemView2(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public SettingItemView2(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        initView(context, attrs, defStyleAttr, defStyleRes);
    }

    private void initView(Context context,  @Nullable AttributeSet attrs, int defStyleAttr, @StyleRes int defStyleRes) {
        LayoutInflater.from(context).inflate(R.layout.layout_setting_item_view, this, true);
        iconImageView = findViewById(R.id.iv_icon);
        titleLabel = findViewById(R.id.tv_title);
        summaryLabel = findViewById(R.id.tv_sub_title);

        @SuppressLint("CustomViewStyleable") TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemView, defStyleAttr, defStyleRes);
        int iconResId = typedArray.getResourceId(R.styleable.SettingItemView_sivIcon, R.drawable.ic_my_friend);
        String titleText = typedArray.getString(R.styleable.SettingItemView_sivTitle);
        String summaryText = typedArray.getString(R.styleable.SettingItemView_sivSummary);
        int titleColor = typedArray.getColor(R.styleable.SettingItemView_sivTitleColor, getResources().getColor(R.color.color_title));
        int summaryColor = typedArray.getColor(R.styleable.SettingItemView_sivSummaryColor, getResources().getColor(R.color.color_summary));
        if (titleText == null) {
            titleText = "";
        }
        if (summaryText == null) {
            summaryText = "";
        }

        iconImageView.setImageResource(iconResId);
        titleLabel.setText(titleText);
        summaryLabel.setText(summaryText);
        titleLabel.setTextColor(titleColor);
        summaryLabel.setTextColor(summaryColor);

        typedArray.recycle();
    }

    public void setIconResource(int resId) {
        iconImageView.setImageResource(resId);
    }

    public void setTitle(String text) {
        titleLabel.setText(text);
    }

    public void setSummary(String text) {
        summaryLabel.setText(text);
    }

    public void setTitleColor(int color) {
        titleLabel.setTextColor(color);
    }

    public void setSummaryColor(int color) {
        summaryLabel.setTextColor(color);
    }
}
